import { apiService } from './api'
import { API_ENDPOINTS } from '@/config/api'

// 任务接口
export interface Task {
  id: string
  name: string
  effective_time: string
  task_type: string
  sn: string
  product_id: string
  version: string
  tenant_id: string
  execute_time: string
  status: string
  created_at: string
  updated_at: string
  product: {
    id: string
    name: string
    public_id: string
  }
}

// 创建任务请求
export interface CreateTaskRequest {
  name: string
  effective_time: string
  task_type: string
  sn?: string
  product_id: string
  version: string
  tenant_id?: string
  execute_time: string
}

// 更新任务请求
export interface UpdateTaskRequest {
  name?: string
  effective_time?: string
  task_type?: string
  sn?: string
  product_id?: string
  version?: string
  tenant_id?: string
  execute_time?: string
  status?: string
}

// 任务服务
export const taskService = {
  // 获取任务列表
  getTasks: (productId?: string, status?: string): Promise<Task[]> => {
    const params: any = {}
    if (productId) params.product_id = productId
    if (status) params.status = status
    return apiService.get<Task[]>(API_ENDPOINTS.TASKS, params)
  },

  // 获取单个任务
  getTask: (id: string): Promise<Task> => {
    return apiService.get<Task>(API_ENDPOINTS.TASK(id))
  },

  // 创建任务
  createTask: (data: CreateTaskRequest): Promise<Task> => {
    return apiService.post<Task>(API_ENDPOINTS.TASKS, data)
  },

  // 更新任务
  updateTask: (id: string, data: UpdateTaskRequest): Promise<Task> => {
    return apiService.put<Task>(API_ENDPOINTS.TASK(id), data)
  },

  // 删除任务
  deleteTask: (id: string): Promise<{ message: string }> => {
    return apiService.delete<{ message: string }>(API_ENDPOINTS.TASK(id))
  }
} 